<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label class="layui-form-label">处理状态</label>
        <div class="layui-inline">
          <el-select v-model="selectData.JmpStatusEquals" filterable placeholder="请选择处理状态">
            <el-option v-for="item in dealStatusOptions" :label="item.Text" :value="item.Value" :key="item.Value">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">殡仪馆</label>
        <div class="layui-inline">
          <el-select v-model="selectData.JmpRePvdnoEquals" filterable placeholder="请选择殡仪馆">
            <el-option v-for="item in funeralOptions" :label="item.ORGNAME" :value="item.ORGID" :key="item.ORGID">
            </el-option>
          </el-select>
        </div>
      </div>
      <!-- <div class="layui-inline" v-if="!showdz">
        <label class="layui-form-label">户籍地址</label>
        <div class="layui-inline">
          <el-input style="width:220px;" v-model="selectData.JpmDetailAddressEquals" placeholder="请输入户籍地址"></el-input>
        </div>
      </div> -->
      <div class="layui-inline" v-if="showdz">
        <label class="layui-form-label">地址</label>
        <div class="layui-input-inline">
          <el-cascader style="width:305px" class="dz" ref="address" v-model="arr" :options="addressOptions"
            change-on-select>
          </el-cascader>
        </div>
      </div>
      <el-button type="primary" @click="getData" :loading="this.$store.state.isloading">搜索</el-button>
      <el-button @click="btnRepeat">重置</el-button>
    </div>
    <div>
      <ul class="toolList">
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon"
          @click="operate(item.BTNVALUE)" :key="index">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <dataTable :headList="headList" :dataList="dataList" :hiddenColumn="false" :pageOrder="pageOrder" @pageChange="pageChange"
      @selectRow="selectRow"></dataTable>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";

  .el-button+.el-button {
    margin-left: 0;
  }

  .longtext {
    width: 480px;
  }

  .longertext {
    width: 770px;
  }

  .sweet-btn {
    margin-top: 20px;
  }

  .radio {
    margin-top: 9px;
  }

</style>
<script>
  import dataTable from "@/components/Table/DataTable";
  import apiBase from "@/api/apiBase.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  export default {
    name: "jointRetentionQuery",
    mixins: [manage],
    data() {
      return {
        isUseNewAddress: null,
        showdz: null,
        arr: [],
        addressOptions: [],
        dealStatusOptions: [],
        funeralOptions: [],
        //户籍所在地
        selectData: {
          JmpStatusEquals: null,
          JmpRePvdnoEquals: null,
        },
      };
    },
    methods: {
      dealData(id, option) {
        let level = "";
        for (let index = 0; index < option.length; index++) {
          if (id == option[index].value) {
            return option[index].level;
          } else if (option[index].children) {
            if (this.dealData(id, option[index].children)) {
              return this.dealData(id, option[index].children);
            }
          }
        }
      },
      //获取火化登记数据
      getData() {
        let data = JSON.parse(JSON.stringify(this.selectData))
        let JpmSaridLike = this.arr[this.arr.length - 1];
        let SARID = this.arr[this.arr.length - 1];
        if (this.isUseNewAddress) {
          JpmSaridLike = null
        } else {
          if (JpmSaridLike) {
            let level = this.dealData(JpmSaridLike, this.addressOptions);
            switch (level) {
              case "3":
                JpmSaridLike = JpmSaridLike.substring(0, 6);
                break;
              case "4":
                JpmSaridLike = JpmSaridLike.substring(0, 9);
                break;
              case "5":
                JpmSaridLike = JpmSaridLike.substring(0, 12);
                break;
              default:
                break;
            }
          }
          SARID = null
        }
        data.SARID = SARID
        apiBase.DoService({
          PageIndex: this.pageOrder.currentPage,
          PageSize: this.pageOrder.pageSize,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: data
        },
          apiBase.getApiAdress().JointRegistration.QueryRetention,
          res => {
            this.dataList = res.data.content.OutData;
            this.pageOrder.totalNum = res.data.content.RecordCount;
          }
        );
      },
      //获取表头
      getColumns() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Column.GetZlsjColumns,
          res => {
            this.headList = res.data.content;
          }
        );
      },
      btnShow(btn) {
        if (btn == "导出") return true;
      },
      //功能操作
      operate(order) {
        this.order = order;
        switch (this.order) {
          case "导出":
            this.exportExcel();
            break;
          default:
        }
      },
      exportExcel() {
        let data = JSON.parse(JSON.stringify(this.selectData))
        let JpmSaridLike = this.arr[this.arr.length - 1];
        let SARID = this.arr[this.arr.length - 1];
        if (this.isUseNewAddress) {
          JpmSaridLike = null
        } else {
          if (JpmSaridLike) {
            let level = this.dealData(JpmSaridLike, this.addressOptions);
            switch (level) {
              case "3":
                JpmSaridLike = JpmSaridLike.substring(0, 6);
                break;
              case "4":
                JpmSaridLike = JpmSaridLike.substring(0, 9);
                break;
              case "5":
                JpmSaridLike = JpmSaridLike.substring(0, 12);
                break;
              default:
                break;
            }
          }
          SARID = null
        }
        data.SARID = SARID
        apiBase.DoService({
            PageIndex: -1,
            Data: data
          },
          apiBase.getApiAdress().JointRegistration.QueryRetention,
          res => {
            let dataAll = res.data.content.OutData;
            if (dataAll.length === 0) {
              this.$message.error("无导出数据！");
              return;
            }
            this.downloadLoading = true;
            require.ensure([], () => {
              const {
                export_json_to_excel
              } = require("@/vendor/Export2Excel");
              export_json_to_excel(this.headList, dataAll, "滞留数据");
              this.downloadLoading = false;
            });
          }
        );
      },

      //获取地址

      restructur(value) {
        let arr = JSON.parse(JSON.stringify(value));
        var data = [];
        arr.forEach(item => {
          if (item.Children.length > 0) {
            data.push({
              label: item.SARNAME,
              value: item.SARID,
              level: item.SARLEVEL,
              children: this.restructur(item.Children)
            });
          } else {
            data.push({
              label: item.SARNAME,
              value: item.SARID,
              level: item.SARLEVEL
            });
          }
        });
        return data;
      },

      //查询条件重置
      btnRepeat() {
        this.arr = [];
        this.selectData = {}
        this.getData();
      },
    },
    mounted() {},
    components: {
      dataTable
    },
    activated() {
      this.getData();
      //控制地址下拉显隐
      apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsBySar, res => {
        this.showdz = res.data.content;
      });
      // 获取级联选择
      apiBase.DoService({}, apiBase.getApiAdress().Code.GetBoroughByCode, res => {
        let data = res.data.content;
        this.addressOptions = data.map(item => ({
          label: item.SARNAME,
          value: item.SARID,
          level: item.SARLEVEL,
          children: item.Children.map(item => ({
            label: item.SARNAME,
            value: item.SARID,
            level: item.SARLEVEL,
            children: this.restructur(item.Children)
          }))
        }));
      });
    },
    created() {
      this.getColumns();
      //是否启用新的联办地点查询（舟山联办）
      apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsUseNewAddress, res => {
        this.isUseNewAddress = res.data.content;
      });
      // 获取火化殡仪馆
      apiBase.DoService({}, apiBase.getApiAdress().JointRegistration.QueryOrgan, res => {
        this.funeralOptions = res.data.content;
      });
      // 获取处理状态
      apiBase.DoService({}, apiBase.getApiAdress().JointRegistration.GetJmpStatus, res => {
        this.dealStatusOptions = res.data.content;
      });
    },
    watch: {}
  };

</script>
